const Base = require('./base.js');
const urls = require('url');
module.exports = class extends Base {
	constructor(ctx) {
		super(ctx);
		this.goods = this.model("taoke/goods");
		this.qrcode = this.model("taoke/qrcode");
		this.transfer = this.model("taoke/transfer");
		this.user = this.model("system/user");
		this.wbuser = this.appModel("user", "wxbot");
	}
	async indexAction() {

		if(this.isGet) {
			let gets = this.get();
let querystring = this.ctx.querystring;
			if(querystring.indexOf('&amp;') > -1) {

				gets = urls.parse(this.ctx.href.replace(/&amp;/g, '&'), true).query;
			}
			if(querystring.indexOf('*|*') > -1) {

				gets = urls.parse(this.ctx.href.replace(/\*\|\*/g, '&'), true).query;
			}

			this.assign("wxid", gets.wxid);
			return this.appDisplay();
		}

		if(this.isPost) {
			let posts = this.post();
			console.log(posts);
			let map = {};
			let data ={
				count: 0,
				data:[]
			};
			if(posts.keyword.length < 10){
			if(!think.isEmpty(posts.keyword)) {
				map = {
					'title': ['like', '%' + posts.keyword + '%']
				}
			}
			if(!think.isEmpty(posts.cid)) {
				map.cid = Number(posts.cid);
			}
			let sort = {};
			if(posts.sort == '0') {
				sort = {
					quan_time: 'ASC',
					sales_num: 'DESC',
					quan_price: 'DESC',
					price: 'ASC'
				}
			}
			if(posts.sort == '1') {
				sort = {
					price: 'ASC'
				}
			}
			if(posts.sort == '2') {
				sort = {
					quan_price: 'DESC'
				}
			}
			if(posts.sort == '3') {
				sort = {
					sales_num: 'DESC'
				}
			}

			data = await this.goods.where(map).page(posts.pageNum, posts.pageSize).order(sort).countSelect();
			}
			if(data.count == 0  || posts.keyword.length >= 10) {
				let q = posts.keyword;
				let cat = '';
				let sort = '';
				if(posts.sort == '0') {
					sort = 'tk_total_commi_des';
				}
				if(posts.sort == '1') {
					sort = 'price_asc';
				}
				if(posts.sort == '2') {
					sort = 'tk_rate_des';
				}
				if(posts.sort == '3') {
					sort = 'total_sales_des';
				}
				const alimama = this.service("alimama");
				let res = await alimama.search(q, cat, sort, posts.pageSize, posts.pageNum);
				console.log(res);
				if(res.result_list){
					
				
				data.pageSize = posts.pageSize;
				data.currentPage = posts.pageNum;
				data.count = res.total_results;
				data.totalPages = Math.ceil(data.count / data.pageSize);
				let goods = res.result_list.map_data;
				for(let i in goods) {
					goods[i].coupon_amout = 0;
					if(goods[i].coupon_info) {
						goods[i].coupon_condition = (goods[i].coupon_info).match(/\u6EE1[0-9\.]{1,7}\u5143/)[0].slice(1, -1);
						goods[i].coupon_amout = (goods[i].coupon_info).match(/\u51CF[0-9\.]{1,7}\u5143/)[0].slice(1, -1);
					}

					data.data.push({
						d_title: goods[i].short_title,
						title: goods[i].title,
						quan_price: goods[i].coupon_amout,
						quan_time: goods[i].coupon_end_time,
						price: (goods[i].zk_final_price - goods[i].coupon_amout).toFixed(2),
						sales_num: goods[i].volume,
						introduce: goods[i].title,
						istmall: goods[i].user_type,
						goods_id: goods[i].num_iid,
						pic: goods[i].pict_url,
						org_price: goods[i].zk_final_price,
						quan_id: goods[i].coupon_id,
						quan_condition: goods[i].coupon_condition,
						quan_surplus: goods[i].coupon_remain_count,
						seller_id: goods[i].seller_id
					});

				}
				
				}

			}

			return this.success(data);

		}

	}
	async detailAction() {
		let gets = this.get();
		let querystring = this.ctx.querystring;
		if(querystring.indexOf('&amp;') > -1) {

			gets = urls.parse(this.ctx.href.replace(/&amp;/g, '&'), true).query;
		}
		if(querystring.indexOf('*|*') > -1) {

			gets = urls.parse(this.ctx.href.replace(/\*\|\*/g, '&'), true).query;
		}

		let user = await this.wbuser.where({
			wxid: gets.wxid
		}).find();
		let level = await this.user.where({
			id: user.user_id || ''
		}).getField("level", true) || 1;
		let goods = await this.transfer.getTaowordByGoodsId(gets.goods_id, level);

		goods.org_price = goods.goods.zk_final_price;
		goods.sales_num = goods.goods.volume;
		goods.title = goods.goods.title;
		goods.pic = goods.goods.pict_url;
		goods.url = goods.coupon_click_url;
		console.log(goods)
		if(gets.source != 1) {
			await this.transfer.add({
				"title": goods.title,
				"org_price": goods.org_price,
				"price": goods.final_price,
				"quan_price": goods.coupon_amout,
				"goods_id": goods.goods.num_iid,
				"commission": goods.max_commission_rate,
				"pic": goods.goods.pict_url,
				"tao_word": goods.kouling,
				"user_id": user.user_id,
				"create_time": new Date().valueOf()
			});
		}

		this.assign("goods", goods);
		return this.appDisplay();
	}
	
	
	helpAction(){
		return this.appDisplay();
	}

};